/**
 * Author(s): xuming.Wong <xuming.Wong@gmail.com>
 */
#include "MapDatabase.h"
#include "MapDbc.h"
#include "WOWArchive.h"

namespace PQGame
{
	WdtFile* MapDatabase::getWdtFile(int index)
	{
		MapInfo info=SMapDbc.getMapInfo(index);

		char name[256];
		sprintf(name,"World\\Maps\\%s\\%s.wdt", info.Name.c_str(),  info.Name.c_str());
		
		MPQFile* f=SWOWArchive.openMPQFile(name);
		if(f){
			WdtFile* wdt=new WdtFile(f);
			return wdt;
		}
		return 0;
	}

	WdlFile* MapDatabase::getWdlFile(int index)
	{
		MapInfo info=SMapDbc.getMapInfo(index);

		char name[256];
		sprintf(name,"World\\Maps\\%s\\%s.wdl", info.Name.c_str(),  info.Name.c_str());

		MPQFile* f=SWOWArchive.openMPQFile(name);
		if(f){
			WdlFile* wdl=new WdlFile(f);
			return wdl;//should we keep a easy search pointer?
		}
		return 0;
	}

	AdtFile* MapDatabase::getAdtFile(int index,int col,int row)
	{
		int cnt=SMapDbc.getMapCount();
		
		if(index>=0&&index<cnt){
			if(col>=0 && row >= 0 && col<64 && row<64){
				MapInfo info=SMapDbc.getMapInfo(index);
				char name[256];
				sprintf(name,"World\\Maps\\%s\\%s_%d_%d.adt", info.Name.c_str(), info.Name.c_str(), col, row);

				MPQFile* f=SWOWArchive.openMPQFile(name);
				if(f){
					AdtFile* adt=new AdtFile(f);
					return adt;//should we keep a easy search pointer?
				}
			}
		}
		return 0;
	}

}
